#include "Camera.h"
#include "Log.h"
namespace GL
{
    Camera::Camera()
    {
    }

    Camera::~Camera()
    {
    }

    glm::mat4 Camera::LookAt(glm::vec3 &target)
    {

        return glm::lookAt(position, target, UpDirection);
    }

    void Camera::SetSpeed(float speed)
    {
        m_speed = speed;
    }
    void Camera::SetCursorPoint(double xPos, double yPos)
    {

        xpos = xPos;
        ypos = yPos;
    };

    void Camera::SetFront()
    {
        if (firstMouse)
        {
            lastX = xpos;
            lastY = ypos;
            firstMouse = false;
        }

        float xoffset = xpos - lastX;
        float yoffset = lastY - ypos;
        lastX = xpos;
        lastY = ypos;

        float sensitivity = 0.1f;
        xoffset *= sensitivity;
        yoffset *= sensitivity;
        GL_INFO("xoffset , {0}", xoffset);

        yaw += xoffset;

        pitch += yoffset;

        if (pitch > 89.0f)
            pitch = 89.0f;
        if (pitch < -89.0f)
            pitch = -89.0f;

        glm::vec3 front;
        front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
        front.y = sin(glm::radians(pitch));
        front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
        FrontDirection = glm::normalize(front);
    }

    void Camera::Update()
    {
        SetFront();

        glm::vec3 target = position + FrontDirection;
        m_view = LookAt(target);
    }

    void Camera::Move(Axis axis)
    {

        switch (axis)
        {
        case Axis::Z:
            position += m_speed * FrontDirection;
            break;
        case Axis::z:
            position -= m_speed * FrontDirection;
            break;
        case Axis::X:
            position += glm::normalize(glm::cross(FrontDirection, UpDirection)) * m_speed;
            break;
        case Axis::x:
            position -= glm::normalize(glm::cross(FrontDirection, UpDirection)) * m_speed;
            break;
        }
    }
}